;;; init-company.el --- company config -*- lexical-binding:t -*-
;;; Commentary:
;;; Code:

;; compnay 补全框架
(use-package company
  :defer t
  :hook (after-init . global-company-mode)
  :diminish company-mode
  :bind (("M-C-/" . company-complete)
         :map company-mode-map
         ("M-/" . company-complete)
         ([remap completion-at-point] . company-complete)
         ([remap indent-for-tab-command] . company-indent-or-complete-common)
         :map company-active-map
         ("M-/" . company-other-backend)
         ("C-n" . company-select-next)
         ("C-p" . company-select-previous)
         ("C-d" . company-show-doc-buffer)
         ("M-." . company-show-location)
         ("RET" . company-complete-selection)
         ("<tab>" . company-complete-common-or-cycle)
         ("<backtab>" . my/company-yasnippet)
         )
  :init
  (defun my/company-yasnippet ()
    "Hide the current completions and sho snippets"
    (interactive)
    (company-abort)
    (call-interactively 'company-yasnippet))

  :config
  (setq
   company-tooltip-align-annotations t
   company-tooltip-limit 12
   company-idle-delay 0
   company-echo-delay (if (display-graphic-p) nil 0)
   company-minimum-prefix-length 2
   company-require-match nil
   )

  (defun company-yasnippet/disable-after-dot (fun command &optional arg &rest _ignore)
    (if (eq command 'prefix)
        (let ((prefix (funcall fun 'prefix)))
          (when (and prefix (not
                             (eq
                              (char-before (- (point) (length prefix)))
                              ?.)))
            prefix))
      (funcall fun command arg)))

  (advice-add #'company-yasnippet :around #'company-yasnippet/disable-after-dot)
  )

;; company-prescient 候选人排序
(use-package company-prescient
  :defer t
  :after company
  :hook (company-mode . company-prescient-mode))

;; quickhelp company快速帮助
(use-package company-quickhelp
  :defer t
  :after company
  :hook (global-company-mode . company-quickhelp-mode)
  :defines company-quickhelp-delay
  :bind (:map company-active-map
              ([remap company-show-doc-buffer] . company-quickhelp-manual-begin))
  :init (setq company-quickhelp-delay 0.5)
  )

(provide 'init-company)
;;; init-company.el ends here
